package org.linuxgears.BouncingBall.model.context;

import java.awt.Point;
import java.awt.Rectangle;

import org.linuxgears.BouncingBall.mover.strategy.ProgressiveMovingStrategy;

public class BorderBounceMutator implements ContextMutator<BallContext> {

	private Border border;

	public BorderBounceMutator(Border border) {
		this.border = border;
	}
	

	@Override
	public void mutate(BallContext xtc) {
		// bouncing is applicable only to objects that move progressively
		if (! (xtc.getMover().getMovingStrategy() instanceof ProgressiveMovingStrategy)) return;

		ProgressiveMovingStrategy pms = (ProgressiveMovingStrategy) xtc.getMover().getMovingStrategy();
		Point position = xtc.getPosition();

		Point velocity = pms.getVelocity();
		
		Rectangle bounds = xtc.getBounds();
		int size = xtc.getMovable().getSize();
		
		switch (border) {
		case RIGHT:
            position.x = bounds.width - size;
            velocity.x = -velocity.x;
            break;
		case LEFT:
            position.x = 0;
            velocity.x = -velocity.x;				
            break;			
		case BOTTOM:
            position.y = bounds.height - size;
            velocity.y = -velocity.y;
            break;
		case TOP:
            position.y = 0;
            velocity.y = -velocity.y;				
            break;
		}
		
		pms.setVelocity(velocity);
		xtc.setPosition(position);
	}
	
}
